package cn.beyond.vehicleManagement.controller;
import cn.beyond.vehicleManagement.pojo.Vehicles;
import cn.beyond.vehicleManagement.service.VehicleService;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet(urlPatterns = {"/VehicleServlet"})
public class VehicleServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //定义全局变量
        VehicleService service=new VehicleService();
        String opr= req.getParameter("opr");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        Vehicles vehicles=new Vehicles();
        Map<String,Object> map = new HashMap<>();
        PrintWriter out= resp.getWriter();
        List<Vehicles> vehiclesList=new ArrayList<>();
        System.out.println(opr);
        switch (opr){
//            case "addvehicleProcurement":
//                boolean addflag= addUser(req,userService);
//                out.print(addflag);
//                break;
            //删除车辆采购记录
            case "deletevehicle":
                int delId=Integer.parseInt(req.getParameter("Id"));
                boolean delflag=false;
                int delcount=service.deleteVehicle(delId,"id");
                System.out.println(delcount);
                if(delcount>0){
                    delflag=true;
                }
                out.print(delflag);
                break;
//            case "updatevehicleProcurement":
//                updateUser(req,userService,out);
//
//                break;
            case "selectvehicleProcurement":

                break;
            case "listvehicle":
                //列举出采购列表
                vehiclesList=service.selectUserByCondition(vehicles,map);
                String listJosn= JSON.toJSONString(vehiclesList);
                out.write(listJosn);
                map.clear();
                break;

        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req,resp);
    }
}
